#include "ship.h"
#include "projectile.h"
#include "global.h"




//--------------------------------------------

ship::ship(){
    this->distance = SCREEN_W/10;
    this->size = int(this->distance*.18);
    this->health = 3;
    this->speed = SCREEN_W * .00012;
    this->width = .2;
    this->cur_proj = 0;
    this->cur_bomb = 0;
    this->cur_def = 0;
    this->theta = 0;
    this->max_weapon = 1;
    this->proj_count[0] = -1;
    this->proj_count[1] = 0;
    this->proj_count[2]= 0;
    this->lives = 3;
    this->energy = 0;

    this->nx  = int((this->distance * cos(this->theta)) + SCREEN_W/2);
    this->ny  = int((this->distance * sin(this->theta)) + SCREEN_H/2);
    this->lx = int(((this->distance - this->size) * cos(this->theta-(this->width*.50))) + SCREEN_W/2);
    this->ly = int(((this->distance - this->size) * sin(this->theta-(this->width*.50))) + SCREEN_H/2);
    this->rx = int(((this->distance - this->size) * cos(this->theta+(this->width*.50))) + SCREEN_W/2);
    this->ry = int(((this->distance - this->size) * sin(this->theta+(this->width*.50))) + SCREEN_H/2);
}

void ship::move(){
    int mickeyx = 0;
    int mickeyy = 0;
    get_mouse_mickeys(&mickeyx, &mickeyy);

    if ((mickeyx > 0 ) || (key[KEY_RIGHT])){
        this->theta = this->theta + sin(this->speed);
    }
    if ((mickeyx < 0 ) || (key[KEY_LEFT])){
        this->theta = this->theta - sin(this->speed);
    }
    this->nx  = int((this->distance * cos(this->theta)) + SCREEN_W/2);
    this->ny  = int((this->distance * sin(this->theta)) + SCREEN_H/2);
    this->lx = int(((this->distance - this->size) * cos(this->theta-(this->width*.50))) + SCREEN_W/2);
    this->ly = int(((this->distance - this->size) * sin(this->theta-(this->width*.50))) + SCREEN_H/2);
    this->rx = int(((this->distance - this->size) * cos(this->theta+(this->width*.50))) + SCREEN_W/2);
    this->ry = int(((this->distance - this->size) * sin(this->theta+(this->width*.50))) + SCREEN_H/2);
}

int ship::set_cur_weapon(){
    if ((key[KEY_Q]) || ( (this->get_cur_proj() == 1 && this->get_proj_count(1) == 0) ||
        (this->get_cur_proj() == 2 && this->get_proj_count(2) == 0) ) )
        this->cur_proj = 0;
    if (key[KEY_W])
        this->cur_proj = 1;
    if (key[KEY_E])
        this->cur_proj = 2;
    if (key[KEY_A])
        this->cur_bomb = 0;
    if (key[KEY_S])
        this->cur_bomb = 1;
    if (key[KEY_D])
        this->cur_bomb = 2;
    if (key[KEY_Z])
        this->cur_def = 0;
    if (key[KEY_X])
        this->cur_def = 1;
    if (key[KEY_C])
        this->cur_def = 2;
}

//--------------------------------------------------------------------------------------------------------
void ship::shoot(BITMAP* buffer, ship *player, projectile* bullet, projectile* spread[], projectile* laser){
    switch(this->cur_proj){
        case 0:
            bullet->set_x(this->nx);
            bullet->set_y(this->ny);
            bullet->set_distance(this->distance);
            bullet->set_exists(true);
            bullet->set_speed(proj_spd);
            bullet->set_theta(this->theta);
            bullet_fired = 1;
            play_sample(shoot_sound, 255, 127, 1000, 0);
            break;
        case 1:
            if(this->get_proj_count(1) > 0){
                for(int i=0;i<3;i++){
                    spread[i]->set_x(player->get_nx());
                    spread[i]->set_y(player->get_ny());
                    spread[i]->set_distance(player->get_distance());
                    spread[i]->set_exists(true);
                    spread[i]->set_speed(proj_spd);
                }
                spread[1]->set_theta(player->get_theta());
                spread[0]->set_theta(spread[1]->get_theta() - .15);
                spread[2]->set_theta(spread[1]->get_theta() + .15);
                bullet_fired = 3;
                if(player->get_proj_count(1) > 0){
                    player->set_proj_count(spread[0]->get_type(), -1);
                }
            }
            play_sample(shoot_sound, 255, 127, 1000, 0);
            play_sample(shoot_sound, 255, 127, 1000, 0);
            play_sample(shoot_sound, 255, 127, 1000, 0);
            break;
        case 2:
            if(this->get_proj_count(2) >0){
                laser->set_x(player->get_nx());
                laser->set_y(player->get_ny());
                laser->set_distance(player->get_distance());
                laser->set_exists(true);
                laser->set_speed(proj_spd);
                laser->set_theta(player->get_theta());
                laser->set_size(16);
                bullet_fired = 1;
                if(player->get_proj_count(2) > 0){
                    player->set_proj_count(laser->get_type(), -1);
                }
                play_sample(laser_sound, 255, 127, 1000, 0);
            }
            break;
    }
}

//--------------------------------------------------------------------------------------------------------
void ship::draw(BITMAP* buffer){
    int color;

    if(this->health == 1)
        color = RED;
    else if (this->health == 2)
        color = YELLOW;
    else if (this->health == 3)
        color = GREEN;
    else
        color = WHITE;

    line(buffer, this->nx, this->ny, this->lx, this->ly, color);
    line(buffer, this->nx, this->ny, this->rx, this->ry, color);
    line(buffer, this->lx, this->ly, this->rx, this->ry, color);
}
//---------------------------------------------------------------------------------------------------------
void ship::display_energy(BITMAP* buffer){
    switch(this->energy){
        case 0:
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.05), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.03), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.08), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.06), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.11), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.09), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.14), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.12), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.17), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.15), GREEN);
            break;
        case 1:
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.05), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.03), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.08), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.06), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.11), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.09), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.14), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.12), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.17), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.15), GREEN);
            break;
        case 2:
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.05), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.03), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.08), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.06), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.11), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.09), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.14), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.12), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.17), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.15), GREEN);
            break;
        case 3:
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.05), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.03), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.08), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.06), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.11), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.09), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.14), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.12), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.17), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.15), GREEN);
            break;
        case 4:
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.05), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.03), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.08), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.06), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.11), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.09), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.14), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.12), GREEN);
            rect(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.17), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.15), GREEN);
            break;
        case 5:
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.05), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.03), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.08), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.06), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.11), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.09), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.14), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.12), GREEN);
            rectfill(buffer, int(SCREEN_W-SCREEN_W*.08), int(SCREEN_H-SCREEN_H*.17), int(SCREEN_W-SCREEN_W*.03), int(SCREEN_H - SCREEN_H *.15), GREEN);
            break;
    }


}
